
<?php

/**
 * @Author:      lyt
 * @DateTime:  2017-03-25 15:33:24
 * @Description: 洗车单的退款
 */
class ReturnCarwashOrder extends Eloquent
{
    protected $table = "return_carwash_order";
    
    public static $reason = array(
        "0"=>"无理由退货",
    );
    public static $accredit_type = array(
        "0"=>"账号授权",
        "1"=>"动态授权",
    );   


    /**
     * @Author:      lyt
     * @DateTime:  2017-03-27 16:17:03
     * @Description: 订单的退款
     */
    public static function refund($return_order_id,$approver,$approver_name){
	    return DB::transaction(function () use ($return_order_id,$approver,$approver_name) {
	        $ro=ReturnCarwashOrder::find($return_order_id);//查找洗车退款单
	        $order=CarWashOrder::find($ro->order_id);
	        $user = Users::find($order->user_id);
	        $ror=CarWashOrderPayRecord::where("order_id",$order->order_id)->first();//找到原洗车单的支付记录
	        
	        // $tran_no = date('Ymd',time()).Common::generateCode();
          $tran_no = date('YmdHis',time()).Common::generateCode(8);
	        $order_amount=$ror->amount;
	        $rwor=ReturnWashOrderRecord::where("return_washorder_id",$ro->id)->first();
	        //各种进行判断
	        if($ror->pay_type== 1 || $ror->pay_type== 7||$ror->pay_type== 3||$ror->pay_type== 2){//现金 或者银行卡支付 或者微信支付
	            //记录公司账户收入
	            // Account::record_base('1',$tran_no,2,'0107',$order_amount,'洗车单订单退货支出');
	            // //记录订单账务关系
	            // CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));

	              Account::record($order->user_id,$order->shop_id,1,$tran_no,1,117,$order_amount,'洗车单退单收入,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
                //记录用户收入
                Account::record($order->user_id,$order->shop_id,1,$tran_no,2,217,$order_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
                //记录公司账户支出
                Account::record_base('1',$tran_no,2,217,$order_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
                //记录订单账务关系
                CarWashOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
	        }else if($ror->pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空
                $arr=AcountUserRechargeSpend::getUseRecharge($ror->id,$order_amount,2);
                  if(sizeof($arr)>0){
                    foreach ($arr as $_arr) {
                      //记录了用户消费退款记录
                      $aurs=new AcountUserRechargeSpendRe;
                      $aurs->aurr_id=$_arr->aurr_id;
                      $aurs->user_id=$_arr->user_id;
                      $aurs->aurr_sp_id=$_arr->id;                      
                      $aurs->buss_id=$rwor->id;
                      $aurs->type=2;//洗车单退款
                      $aurs->amount=$_arr->user_amount;
                      $aurs->status=1;
                      $aurs->tran_no=$tran_no;
                      $aurs->shop_id=$order->shop_id;
                      $aurs->implement_time=date("Y-m-d H:i:s",time());
                      $aurs->save();
                      //更改充值记录
                      DB::update('update acount_user_recharge_record set current_amount = current_amount+? where id = ?', array($_arr->user_amount,$_arr->aurr_id));
                      //更改充值记录消费记录
                      AcountUserRechargeSpend::where('id',$_arr->id)->update(array('return_amount'=> $_arr->return_amount +$_arr->user_amount,));
                      //记录个人 各种账户余额
                      $sac=AcountUserRecharge::find($_arr->aurr_id);
                      Account::record($order->user_id,$sac->shop_id,$_arr->acount_type,$tran_no,1,117,$_arr->user_amount,'洗车单退单收入,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
                      //记录公司账户支出
                      $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                	    Account::record_base($base_acount_num,$tran_no,2,217,$_arr->user_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
                      
                    }
                    CarWashOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
                  }else{
                    DB::rollback();
                    $response = Lang::get("scs_error.order_user_notpay_40039");
                    return $response;
                  } 
          }else if($ror->pay_type== 6){//企业挂账
              Account::record($order->user_id,0,3,$tran_no,1,117,$order_amount,'洗车单退单收入,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
              CarWashOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
          }else{
            Account::record($order->user_id,$order->shop_id,1,$tran_no,1,117,$order_amount,'洗车单退单收入,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
            //记录用户收入
            Account::record($order->user_id,$order->shop_id,1,$tran_no,2,217,$order_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
            //记录公司账户支出
            Account::record_base('1',$tran_no,2,217,$order_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
            //记录订单账务关系
            CarWashOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
            if($ror->pay_type== 50){
              //更新使用状态 
                $scc=ServiceCardConsumption::where("order_id",$ro->order_id)->where("type",1)->first();
                $scc->status=3;
                $scc->save();
                //回退套餐卡次数
                DB::table("service_card_detail_pack")->where("id",$scc->service_card_detail_pack_id)->increment("curr_number");
                $sccr=new ServiceCardConsumptionReturn();
                $sccr->order_id=$scc->order_id;
                $sccr->return_order_id=$ro->id;
                $sccr->service_card_detail_pack_id=$scc->service_card_detail_pack_id;
                $sccr->type=$scc->type;
                $sccr->product_id=$scc->product_id;
                $sccr->save();
            }
          }

          if(in_array($ror->pay_type,CarWashOrderPayRecord::$ei_pay_type)){//说明是返回积分的支付类型
            $mc=MemberCoefficient::where("source",4)->where("status",1)->first();
            $ms=MaintenanceShops::find($order->shop_id);

            UserIntegralFlow::record($order->user_id,$order->shop_id,$ro->id,2,2004,floor($order_amount*$mc->integral*$ms->integral/100),'洗车订单退款扣除积分');
            UserExperienceFlow::record($order->user_id,$order->shop_id,$ro->id,2,2004,floor($order_amount*$mc->experience*$ms->experience/100),'洗车订单退款扣除经验');
            Users::checkMember($order->user_id);
          }
	        $ro->approver=$approver;
	        $ro->approver_name=$approver_name;
	        $ro->status=1;
	        $ro->implement_time=date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff"));
	        $ro->save();
	        return array('code' => 0, 'msg' => 'ok', 'result' => array("return_order_id"=>$return_order_id));
	    });
    }
}